package com.zbl.zblproject.core.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zbl.zblproject.core.entity.Order;
import com.zbl.zblproject.erp.entity.ErpTableEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/11
 */
@Repository
public interface OrderMapper extends BaseMapper<Order> {


    /**
     * 查询客户订单根据时间和查询条件
     * @param start
     * @param end
     * @param search
     * @return
     */
    List<Order> selectOrderBySearchAndTime(@Param("start") String start,
                                           @Param("end") String end,
                                           @Param("search") String search);

    Order selectByOrderSn(String orderSn);

    int updateOrderByOp(@Param("orderSn") String orderSn, @Param("list") List<String> list);

    @Update("UPDATE zbl_orders SET total_money = #{total} WHERE order_sn = #{orderSn}")
    boolean updateOrderTotal(@Param("orderSn") String orderSn, @Param("total") BigDecimal total);

    @Update("UPDATE zbl_orders SET total_money = total_money - #{total} WHERE order_sn = #{orderSn}")
    boolean updateOrderTotalSubMoney(@Param("orderSn") String orderSn, @Param("total") BigDecimal total);


    @Select("SELECT  order_sn FROM zbl_orders WHERE po_sn = #{orderSn}")
    List<String> selectOrderSnByOp(@Param("orderSn") String orderSn);

    @Select("SELECT count(*) FROM zbl_orders WHERE order_sn = #{orderSn}")
    int checkIsExist(@Param("orderSn") String orderSn);

    @Select("SELECT context FROM zbl_orders WHERE order_sn = #{orderSn}")
    String selectOrderContext(@Param("orderSn") String orderSn);

    @Update("UPDATE zbl_orders SET is_confirm = #{confirm}, financial_confirm = #{confirm}   WHERE order_sn = #{orderSn}")
    boolean updateOrderConfirm(@Param("orderSn") String orderSn, @Param("confirm") int status);


    @Update("UPDATE zbl_orders SET context = #{context} WHERE order_sn = #{orderSn}")
    boolean updateOrderContext(@Param("orderSn") String orderSn, @Param("context") String context);

    @Update("UPDATE zbl_orders SET status = #{status} WHERE order_sn = #{orderSn}")
    boolean updateOrderStatus(@Param("orderSn") String orderSn, @Param("status") int status);


    @Update("UPDATE zbl_orders SET status = 3 WHERE order_sn = #{orderSn}")
    boolean orderEnded(@Param("orderSn") String orderSn);


    List<Order> selectStoreConfirmByDeliver(@Param("deliver") int deliver);

    List<Order> selectOrdersByCanUse(@Param("deliver") int deliver);

    List<Order> selectSupplierOrdersByCanUse(@Param("deliver") int deliver);

    /**
     * 根据订单号查询供应商名称
     * @param orderSn
     * @return
     */
    String selectSupplierNameByOrderSn(@Param("orderSn") String orderSn );

    /**
     * 更新订单项下所有的is_nopur字段
     * @param orderSn 订单号
     * @param isNopur isNopur
     * @return
     */
    @Update("UPDATE  zbl_orderitem  SET is_nopur = #{isNopur}  WHERE order_sn = #{orderSn}  ")
    boolean updateOrderItemIsNopur(@Param("orderSn") String orderSn, @Param("isNopur") int isNopur);


    List<String> selectOrderSnIsCgdd(@Param("supplierId") int supplierId, @Param("start") long start, @Param("end") long end);

    @Select("SELECT po_sn FROM zbl_orders WHERE order_sn = #{orderSn} ")
    String selectSnPoByOrderSn(@Param("orderSn") String orderSn);
}
